package cn.usian.service.impl;

import cn.usian.common.R;
import cn.usian.entity.Dish;
import cn.usian.entity.DishDto;
import cn.usian.entity.DishFlavor;
import cn.usian.mapper.DishFlavorMapper;
import cn.usian.mapper.DishMapper;
import cn.usian.service.DishService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Resource
    private DishMapper dishMapper;
    @Resource
    private DishFlavorMapper dishFlavorMapper;
    @Override
    public R<Page> page(int page, int pageSize, String name) {
//        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.like(name!=null,Dish::getName,name);
        Page<Dish> page1 = new Page<>(page,10);
        Page<DishDto> page2=dishMapper.findPage(page1,name);
        return R.success(page2);
    }

    @Override
    public void delete(Long[] ids) {
        for (Long id : ids) {
            dishMapper.deleteById(id);
        }
    }

    @Override
    public void status(Integer status, Long[] ids) {
        Dish dish = new Dish();
        for (Long id : ids) {
            dish.setId(id);
            dish.setStatus(status);
            dishMapper.updateById(dish);
        }
    }



    @Override
    public void add(DishDto dishDto) {
        dishMapper.insert(dishDto);
    }

    @Override
    public R findByid(Long id) {
        List<DishFlavor> flavor01s=dishFlavorMapper.findByName(id);
        Dish dish = dishMapper.selectById(id);
//        dish(flavor01s);
        return R.success(dish);
    }

    @Override
    public Long selectByName(String name) {
        return dishMapper.selectByName(name);
    }

    @Override
    public List<Dish> list(Long categoryId, String name) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(categoryId!=null,Dish::getCategoryId,categoryId);
        queryWrapper.like(name!=null,Dish::getName,name);
        return dishMapper.selectList(queryWrapper);
    }


}
